package com.ian.controller;

import com.common.util.ResultMsg;
import com.ian.pojo.PerUser;
import com.ian.service.PerUserService;
import com.ian.vo.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户信息
 *
 * @Author IanZy
 * @Date 2020/6/18
 */
@Api("用户信息相关API")
@RestController
public class PerUserController {
    @Autowired
    PerUserService perUserService;

    /**
     * 获取当前用户信息
     *
     * @Author IanZy
     * @Date 2020/6/20
     */
    @ApiOperation("查询当前用户信息")
    @GetMapping(value = "/userInfo", produces = "application/json;charset=UTF-8")
    public ResultMsg userInfo(@RequestParam("mobile") String mobile) {
        PerUser perUser = perUserService.findByTel(mobile);
        return ResultMsg.success("查询用户信息成功~", perUser);
    }

    /**
     * 修改昵称
     *
     * @Author IanZy
     * @Date 2020/6/20
     */
    @ApiOperation("修改昵称")
    @PutMapping(value = "/nickName", produces = "application/json;charset=UTF-8")
    public ResultMsg nickName(@RequestParam("mobile") String mobile, @RequestParam("nickname") String nickname) {
        if (nickname != null) {
            PerUser oldUser = perUserService.findByTel(mobile);
            PerUser newUser = new PerUser();
            newUser.setPerUserId(oldUser.getPerUserId());
            newUser.setPerUserTel(mobile);
            newUser.setPerUserPwd(oldUser.getPerUserPwd());
            newUser.setPerUserAddress(oldUser.getPerUserAddress());
            newUser.setPerUserSex(oldUser.getPerUserSex());
            newUser.setPerUserName(nickname);
            newUser.setPerUserHeadimg(oldUser.getPerUserHeadimg());
            newUser.setPerUserStatus(oldUser.getPerUserStatus());
            perUserService.updateByPrimaryKey(newUser);
            return ResultMsg.success("修改姓名成功~", mobile);
        }
        return ResultMsg.failed("修改姓名失败~");
    }

    /**
     * 编辑用户信息
     *
     * @Author IanZy
     * @Date 2020/6/20
     */
    @ApiOperation("修改个人信息")
    @PutMapping(value = "/editUser", produces = "application/json;charset=UTF-8")
    public ResultMsg editUser(@RequestBody UserInfo userInfo) {
        if (userInfo != null) {
            String mobile = userInfo.getMobile();
            String img = userInfo.getImg();
            String sex = userInfo.getSex();
            String userName = userInfo.getUserName();
            String address = userInfo.getAddress();
            PerUser perUser = new PerUser();
            perUser.setPerUserId(perUserService.findByTel(mobile).getPerUserId());
            perUser.setPerUserTel(mobile);
            perUser.setPerUserPwd(perUserService.findByTel(mobile).getPerUserPwd());
            perUser.setPerUserName(userName);
            perUser.setPerUserHeadimg(img);
            perUser.setPerUserSex(sex);
            perUser.setPerUserAddress(address);
            perUser.setPerUserStatus(perUserService.findByTel(mobile).getPerUserStatus());
            perUserService.updateByPrimaryKey(perUser);
            return ResultMsg.success("修改信息成功~", mobile);
        }
        return ResultMsg.failed("修改信息失败~");
    }

    @ApiOperation("上传头像")
    @PutMapping(value = "headImg", produces = "application/json;charset=UTF-8")
    public ResultMsg headImg(@RequestParam("mobile") String mobile, @RequestParam("path") String path) {
        PerUser old = perUserService.findByTel(mobile);
        PerUser user = new PerUser();
        user.setPerUserStatus(old.getPerUserStatus());
        user.setPerUserAddress(old.getPerUserAddress());
        user.setPerUserHeadimg(path);
        user.setPerUserName(old.getPerUserName());
        user.setPerUserPwd(old.getPerUserPwd());
        user.setPerUserSex(old.getPerUserSex());
        user.setPerUserId(old.getPerUserId());
        user.setPerUserTel(mobile);
        perUserService.updateByTel(user);
        return ResultMsg.success("上传头像成功~", path);
    }
}
